layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
      elem: '#startTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime'
        	,value:new Date()
    });

    /**
     * 运输信息表管理
     */
    var TransportMsg = {
        tableId: "transportMsgTable"
    };

    /**
     * 初始化表格的列
     */
    TransportMsg.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'Primary Key ID'},
            {align: 'center', toolbar: '#tableBar', title: 'Operations',width:150},
            {field: 'createUsername', sort: true, title: 'Sign-in Person'},
            {field: 'privatecarFleetName', title: 'Carrier'},
            {field: 'v1', sort: true, title: 'Plate Number'},
            {field: 'v2', sort: true, title: 'Phone Number'},
            {field: 'content', sort: true, title: 'Location'},
            {field: 'times', sort: true, title: 'Sign-in Time'},
            {field: 'londingTime', sort: true, title: 'Loading Time'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportMsg.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['condition'] = $("#condition").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['carrierId'] = $("#carrierId").val();
        table.reload(TransportMsg.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    TransportMsg.exportExcel = function () {
        var checkRows = table.checkStatus(TransportMsg.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportMsg.tableId,
        url: Feng.ctxPath + '/transportMsg/list_driverSignin',
        page: true,
        limit: 40,
		limits:[60,120,240,360,480,600,1500,3000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportMsg.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportMsg.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TransportMsg.exportExcel();
    });

    //查询库区
    form.on('select(operateId)',function(){
        $("#carrierId").empty();
        $.ajax({
            url:Feng.ctxPath + '/basicCarrier/Querylist',
            dataType:"json",   //返回格式为json
            async: false,
            data:{"operateId":$("#operateId").val()},    //参数值
            type:"post",   //请求方式
            success:function(data){
                if(data.length>0){
                    var str = "<option value=''>Select Carrier</option>";
                    $.each(data,function(key,value){
                        str += "<option value='"+value.id+"'>"+value.carrierName+"</option>";
                    });
                    $("#carrierId").html(str);
                }
            }
        });
        form.render('select');
    });

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportMsg.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportMsg/delete", function (data) {
                Feng.success("Delete successful!");
                table.reload(TransportMsg.tableId);
            }, function (data) {
                Feng.error("Delete failed!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("Are you sure you want to delete?", operation);
    };

    // 工具条点击事件
    table.on('tool(' + TransportMsg.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            TransportMsg.onDeleteItem(data);
        }
    });

});
